package DatabaseBrowser;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import database.ConnectionDialog;
import database.ResultSetTableModel;

public class DatabaseBrowser extends JFrame {

  protected Connection connection;

  protected JComboBox catalogBox;

  protected JComboBox schemaBox;

  protected JComboBox tableBox;

  protected JTable table = new JTable();

  public static void main(String[] args) throws Exception {
    //new sun.jdbc.odbc.JdbcOdbcDriver();
	
    DatabaseBrowser db = new DatabaseBrowser();
  }

  public DatabaseBrowser() throws Exception {
	Class.forName("org.sqlite.JDBC");
    ConnectionDialog cd = new ConnectionDialog(this);
    connection = cd.getConnection();
    Container pane = getContentPane();
    pane.add(getSelectionPanel(), BorderLayout.NORTH);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    refreshTable();
    pane.add(new JScrollPane(table), BorderLayout.CENTER);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 450);

    setVisible(true);
  }

  protected JPanel getSelectionPanel() {
    JPanel panel = new JPanel();
    panel.add(new JLabel("Catalog"));
    panel.add(new JLabel("Schema"));
    panel.add(new JLabel("Table"));

    catalogBox = new JComboBox();
    populateCatalogBox();
    panel.add(catalogBox);
    schemaBox = new JComboBox();
    populateSchemaBox();
    panel.add(schemaBox);
    tableBox = new JComboBox();
    populateTableBox();
    panel.add(tableBox);

    catalogBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent event) {
        String newCatalog = (String) (catalogBox.getSelectedItem());
        try {
          connection.setCatalog(newCatalog);
        } catch (Exception e) {
        }
        populateSchemaBox();
        populateTableBox();
        refreshTable();
      }
    });

    schemaBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent event) {
        populateTableBox();
        refreshTable();
      }
    });

    tableBox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent event) {
        refreshTable();
      }
    });
    return panel;
  }

  protected void populateCatalogBox() {
    try {
      DatabaseMetaData dmd = connection.getMetaData();
      ResultSet rset = dmd.getCatalogs();
      Vector values = new Vector();
      while (rset.next()) {
        values.addElement(rset.getString(1));
      }
      rset.close();
      catalogBox.setModel(new DefaultComboBoxModel(values));
      catalogBox.setSelectedItem(connection.getCatalog());
      catalogBox.setEnabled(values.size() > 0);
    } catch (Exception e) {
      catalogBox.setEnabled(false);
    }
  }

  protected void populateSchemaBox() {
    try {
      DatabaseMetaData dmd = connection.getMetaData();
      ResultSet rset = dmd.getSchemas();
      Vector values = new Vector();
      while (rset.next()) {
        values.addElement(rset.getString(1));
      }
      rset.close();
      schemaBox.setModel(new DefaultComboBoxModel(values));
      schemaBox.setEnabled(values.size() > 0);
    } catch (Exception e) {
      schemaBox.setEnabled(false);
    }
  }

  protected void populateTableBox() {
    try {
      String[] types = { "TABLE" };
      String catalog = connection.getCatalog();
      String schema = (String) (schemaBox.getSelectedItem());
      DatabaseMetaData dmd = connection.getMetaData();
      ResultSet rset = dmd.getTables(catalog, schema, null, types);
      Vector values = new Vector();
      while (rset.next()) {
        values.addElement(rset.getString(3));
      }
      rset.close();
      tableBox.setModel(new DefaultComboBoxModel(values));
      tableBox.setEnabled(values.size() > 0);
    } catch (Exception e) {
      tableBox.setEnabled(false);
    }
  }

  protected void refreshTable() {
    String catalog = (catalogBox.isEnabled() ? catalogBox.getSelectedItem().toString() : null);
    String schema = (schemaBox.isEnabled() ? schemaBox.getSelectedItem().toString() : null);
    String tableName = (String) tableBox.getSelectedItem();
    if (tableName == null) {
      table.setModel(new DefaultTableModel());
      return;
    }
    String selectTable = (schema == null ? "" : schema + ".") + tableName;
    if (selectTable.indexOf(' ') > 0) {
      selectTable = "\"" + selectTable + "\"";
    }
    try {
      Statement stmt = connection.createStatement();
      ResultSet rset = stmt.executeQuery("SELECT * FROM " + selectTable);
      table.setModel(new ResultSetTableModel(rset));
    } catch (Exception e) {
    }
  }



}

